# Contributor: Borys Zhukov <mp5@mp5.im>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: omni <omni+alpine@hack.org>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=ocaml5
provides=ocaml
pkgver=5.1.1
pkgrel=0
pkgdesc="Main implementation of the Caml programming language"
url="https://ocaml.org/"
arch="all"
license="LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception"
depends="$pkgname-runtime=$pkgver-r$pkgrel gcc"
checkdepends="parallel"
subpackages="
	$pkgname-doc
	$pkgname-ocamldoc
	$pkgname-compiler-libs:_compiler_libs
	$pkgname-runtime
	"
source="https://caml.inria.fr/pub/distrib/ocaml-${pkgver%.*}/ocaml-$pkgver.tar.xz"
builddir="$srcdir/ocaml-$pkgver"

# The s390x native compiler is newly added in 5.1.x and still needs some work
case "$CARCH" in
s390x)
	options="$options textrels !check"
	export LDFLAGS="$LDFLAGS -Wl,-z,notext"
	;;
esac

build() {
	local _make _conf
	case "$CARCH" in
	# Current native compiler support in 5.1.x
	# Disabling riscv64 here as it segfaults while building dynlink
	aarch64|s390x|x86_64)
		_make="world.opt"
		_conf="--enable-native-compiler \
			--enable-native-toplevel \
			--disable-installing-bytecode-programs"
		msg "Building native compiler for $CARCH"
		;;
	*)
		_make="world"
		_conf="--disable-native-compiler \
			--disable-native-toplevel \
			--enable-installing-bytecode-programs"
		msg "Building bytecode compiler for $CARCH"
		;;
	esac

	./configure \
		--prefix /usr \
		--bindir /usr/bin \
		--libdir /usr/lib/ocaml \
		--mandir /usr/share/man \
		--docdir /usr/share/doc/$pkgname \
		--disable-installing-source-artifacts \
		$_conf \
		CC="${CC:-gcc}" \
		AS="${CC:-gcc} -c" \
		ASPP="${CC:-gcc} -c"
	make $_make
}

check() {
	make ocamltest
	make -C testsuite parallel
}

package() {
	make DESTDIR="$pkgdir" install
}

ocamldoc() {
	pkgdesc="$pkgdesc (documentation generator)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/ocamldoc*
	amove usr/lib/ocaml/ocamldoc
}

_compiler_libs() {
	pkgdesc="$pkgdesc (compiler libraries)"
	depends="$pkgname=$pkgver-r$pkgrel"
	provides="$pkgname-dev=$pkgver-r$pkgrel"

	amove usr/lib/ocaml/compiler-libs
}

runtime() {
	pkgdesc="$pkgdesc (runtime environment)"
	depends=

	amove usr/bin/ocamlrun*
	amove usr/lib/ocaml/ld.conf

	local i; for i in \
		'*.cmo' \
		'*.cmi' \
		'*.cma' \
		'stublibs' \
		'threads/*.cmi' \
		'threads/*.cma'
	do
		amove "usr/lib/ocaml/$i"
	done
}

sha512sums="
e534881eec10b79141b6598ba6d3f59e32ed46a2c64b90c226bbec199c73cdb70e6e5bfa555c34c385d3d83c15e5115f0ea9a182fdda6547db7bbb416f8ed98f  ocaml-5.1.1.tar.xz
"
